<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" class="form-container " :model="page.query" ref="queryForm">
        <div class="search">
          <el-form-item label="姓名" prop="name">
            <el-input
                placeholder="请输入姓名"
                prefix-icon="el-icon-search"
                v-model="page.query.name"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
                v-model="page.query.startTime"
                type="date"
                placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker
                v-model="page.query.endTime"
                type="date"
                placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input
                placeholder="请输入手机号"
                prefix-icon="el-icon-search"
                v-model="page.query.phone"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="装修意愿" prop="intention">
            <el-select v-model.number="page.query.intention" placeholder="请选择装修意愿">
              <el-option
                  v-for="item in intentionOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="来源" prop="sourceId">
            <el-select v-model.number="page.query.sourceId" placeholder="请选择来源">
              <el-option
                  v-for="item in sourceOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-button icon="el-icon-search" type="success" @click="search">搜索</el-button>
          <el-button icon="el-icon-refresh" type="success" class="reset_btn" @click="reset">重置</el-button>
        </div>
        <div class="func">
          <el-button icon="el-icon-plus" type="success" @click="handleMainCost">主材</el-button>
          <el-button icon="el-icon-plus" type="primary" @click="handleAdd">添加</el-button>
        </div>
      </el-form>

    </el-card>
    <el-table :data="customers"
              highlight-current-row
              style="width: 100%"
              @current-change="handleCustomerChange"
    >

      <el-table-column
          prop="name"
          label="客户姓名"
          width="180"
      >
      </el-table-column>
      <el-table-column
          prop="phone"
          label="手机号"
      >
      </el-table-column>
      <el-table-column
          prop="gender"
          label="性别"
      >
        <template v-slot="scope">
          <span v-if="scope.row.gender===1">男</span>
          <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="nickname"
          label="别名"
      >
      </el-table-column>
      <el-table-column
          prop="engineerAddress"
          label="待装修小区"
      >
        <template v-slot="scope">
          <el-tag v-for="p in scope.row.engineerProjects" :key="p.id">{{ p.address }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="homeAddress"
          label="客户地址"
      >
      </el-table-column>
      <el-table-column
          prop="headerName"
          label="负责人姓名"
      >
      </el-table-column>
      <el-table-column
          prop="designerName"
          label="设计师姓名"
      >
      </el-table-column>
      <el-table-column
          prop="intention"
          label="装修意愿"

      >
        <template v-slot="scope">
          <el-tag v-if="scope.row.intention===1" type="danger">强</el-tag>
          <el-tag v-else-if="scope.row.intention===2" type="success">中</el-tag>
          <el-tag v-else>弱</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="sourceId"
          label="来源"

      >
        <template v-slot="scope">
          <span>{{ sourceMap.get(scope.row.sourceId) }}</span>
        </template>

      </el-table-column>
      <el-table-column
          prop="trackingState"
          label="跟踪状态"

      >
        <template v-slot="scope">
          <span>{{ trackMap.get(scope.row.trackingState) }}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"

      >
      </el-table-column>
      <el-table-column
          label="操作"

      >
        <template v-slot="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <page @handlePage="handlePage" :page="page"></page>
    <el-dialog :title="title" :visible.sync="isShow" width="40%">
      <el-form :model="form" inline ref="customerForm">
        <el-form-item label="客户姓名" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="别名" prop="nickname">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" autocomplete="off" type="tel"></el-input>
        </el-form-item>

        <el-form-item label="待装修小区" prop="engineerProjects" style="display: block">
          <el-tag type="success" v-for="(e,index) in form.engineerProjects"
                  @close="handleClose(index)"
                  :key="index" closable

          >{{ e.address }}
          </el-tag>
          <el-input v-model="form.engineerAddress" autocomplete="off" @keyup.enter.native="addAddress"></el-input>
        </el-form-item>
        <el-form-item label="客户地址" prop="homeAddress">
          <el-input v-model="form.homeAddress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model.number="form.gender" placeholder="请选择性别">
            <el-option
                v-for="item in genderOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户来源" prop="sourceId">
          <el-select v-model.number="form.sourceId" placeholder="请选择客户来源">
            <el-option
                v-for="item in sourceOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="装修意愿" prop="intention">
          <el-select v-model.number="form.intention" placeholder="请选择装修意愿">
            <el-option
                v-for="item in intentionOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟踪状态" prop="trackingState">
          <el-select v-model.number="form.trackingState" placeholder="请选择装跟踪状态">
            <el-option
                v-for="item in trackOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设计师" prop="designerId">
          <el-select v-model.number="form.designerId" placeholder="请选择设计师">
            <el-option
                v-for="item in designerOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="监理" prop="supervisorId">
          <el-select v-model.number="form.supervisorId" placeholder="请选择监理">
            <el-option
                v-for="item in supervisorOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预算审核" prop="reviewerId">
          <el-select v-model.number="form.reviewerId" placeholder="请选择设计师">
            <el-option
                v-for="item in reviewerOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
        title="主材预算"
        :visible.sync="isShowMainCost"
        width="30%"
    >
      <el-form ref="mainBudgetForm" :model="mainBudgetForm" label-width="80px">
        <el-form-item label="施工项目" prop="engineerProjectId">
          <el-select v-model="mainBudgetForm.engineerProjectId" placeholder="请选择施工项目">
            <el-option
                v-for="item in engineerProjectOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="handleMainBudgetCancel">取 消</el-button>
        <el-button type="primary" @click="goMainBudget">确 定</el-button>
    </span>
    </el-dialog>

  </div>
</template>

<script>
import Page from '@/components/page/index.vue'
import { deleteCustomer, editCustomer, getCustomer, saveCustomer } from '@/api/intendedCustomer'
import { getDesignerList, getReviewerList, getSupervisorList } from '@/api/user'
import da from 'element-ui/src/locale/lang/da'
import { listEngineerProjects } from '@/api/engineerProject'

export default {
  name: 'IntendedCustomer',
  data() {
    return {
      isShowMainCost: false,
      mainBudgetForm: {
        customerId: '',
        engineerProjectId: ''
      },
      engineerProjectOptions: [],
      title: '',
      isShow: false,
      form: {
        id: '',
        name: '',
        phone: '',
        realName: '',
        nickname: '',
        gender: '',
        designerId: '',
        homeAddress: '',
        engineerAddress: '',
        engineerProjects: [],
        sourceId: '',
        intention: '',
        trackingState: '',
        supervisorId: '',
        reviewerId: ''
      },
      sourceMap: new Map(),
      designerOptions: [],
      supervisorOptions: [],
      reviewerOptions: [],
      genderOptions: [
        {
          label: '男',
          value: 1
        },
        {
          label: '女',
          value: 2
        }

      ],
      sourceOptions: [
        { label: '抖音', value: 1 },
        { label: '小红书', value: 2 },
        { label: '快手', value: 3 },
        { label: '朋友转介绍', value: 4 },
        { label: '广告', value: 5 },
        { label: '其他', value: 6 }
      ],
      intentionMap: new Map(),
      intentionOptions: [
        { label: '强', value: 1 },
        { label: '中', value: 2 },
        { label: '弱', value: 3 }
      ],
      trackMap: new Map(),
      trackOptions: [
        { label: '新客户', value: 1 },
        { label: '沟通中', value: 2 },
        { label: '已成交', value: 3 },
        { label: '失败/放弃', value: 4 },
        { label: '暂不考虑', value: 5 },
        { label: '待定', value: 6 },
        { label: '失联', value: 7 }
      ],
      page: {
        current: 1,
        size: 3,
        total: 0,
        query: {
          name: '',
          phone: '',
          intention: '',
          sourceId: '',
          startTime: '',
          endTime: ''
        }
      }
    }
  },
  computed: {
    customers() {
      this.page.total = this.$store.state.intendedCustomer.customers.total
      return this.$store.state.intendedCustomer.customers.records
    }
  },
  components: { Page },
  methods: {
    goMainBudget() {
      this.$router.push({
        name: 'MainBudget',
        params:{
          'budgetConfig':this.mainBudgetForm
        }
      })
    },
    handleMainBudgetCancel() {
      this.isShowMainCost = false
      this.$refs.mainBudgetForm.resetFields()
    },
    handleCustomerChange(row) {
      this.mainBudgetForm.customerId = row.id
    },

    handleMainCost() {
      if (this.mainBudgetForm.customerId) {
        listEngineerProjects(this.mainBudgetForm.customerId).then(data => {
          this.engineerProjectOptions = data.map(n => {
            return {
              value: n.id,
              label: n.address
            }
          })
          this.isShowMainCost = true
        })
      } else {
        this.$message({
          type: 'error',
          message:'请先选择一个客户'
        })
      }


    },
    addAddress(e) {
      this.form.engineerProjects.push({
        address: this.form.engineerAddress
      })
      this.form.engineerAddress = ''
    },
    handleClose(index) {
      this.form.engineerProjects.splice(index, 1)
    },
    reset() {
      this.page.current = 1
      this.$refs.queryForm.resetFields()
      this.reloadCustomer()

    },

    handleEdit(id) {
      this.title = '编辑意向客户'
      this.isShow = true
      this.form.engineerAddress = ''
      this.loadDesigner()
      this.loadReviewer()
      this.loadSupervisor()
      getCustomer(id).then(res => {
        this.form = { ...res }
        this.form.createTime = ''
        this.form.lastUpdateTime = ''
      })
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        deleteCustomer(id).then(() => {
          this.reloadCustomer()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    resetData() {
      this.$refs.customerForm.resetFields()
      this.isShow = false
      this.title = ''
      this.reloadCustomer()
    },
    handlePage(flag) {
      this.reloadCustomer(flag)
    },
    search() {

    },
    loadDesigner() {
      getDesignerList().then(data => {
        this.designerOptions = data.map(n => {
          return {
            label: n.realName,
            value: n.id
          }
        })
      })
    },
    loadSupervisor() {
      getSupervisorList().then(data => {
        this.supervisorOptions = data.map(n => {
          return {
            label: n.realName,
            value: n.id
          }
        })
      })
    },
    loadReviewer() {
      getReviewerList().then(data => {
        this.reviewerOptions = data.map(n => {
          return {
            label: n.realName,
            value: n.id
          }
        })
      })
    },
    handleAdd() {
      this.title = '添加客户'
      this.isShow = true
      this.form.id = ''
      this.form.engineerAddress = ''
      this.$nextTick(() => {
        this.$refs.customerForm.resetFields()
      })
      this.loadDesigner()
      this.loadReviewer()
      this.loadSupervisor()

    },
    saveOrUpdate() {
      if (this.form.id) {
        editCustomer(this.form).then(res => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          this.resetData()
        })

      } else {
        saveCustomer(this.form).then(res => {
          this.$message({
            type: 'success',
            message: '保存成功!'
          })
          this.resetData()
        })
      }

    },
    cancel() {
      this.isShow = false
      this.title = ''
      this.$refs.customerForm.resetFields()

    },
    handleOptions() {
      this.sourceOptions.forEach(n => {
        this.sourceMap.set(n.value, n.label)
      })
      this.trackOptions.forEach(n => {
        this.trackMap.set(n.value, n.label)
      })
      this.intentionOptions.forEach(n => {
        this.intentionMap.set(n.value, n.label)
      })

    },
    reloadCustomer(flag = true) {
      if (flag) {
        this.page.current = 1
      }
      return new Promise((resolve, reject) => {
        this.$store.dispatch('intendedCustomer/pageCustomer', this.page).then(() => {
          resolve()
        }).catch((err) => {
          reject(err)

        })
      })
    }
  },
  mounted() {
    this.handleOptions()
    this.reloadCustomer()
  }

}
</script>

<style scoped>
.form-container {
  display: flex;
  justify-content: space-between;
}

.search {
  width: 80%;
}

.func {
}

.reset_btn {
  background-color: #722ed1;
  border: 1px solid #722ed1;
}

.reset_btn:hover {
  background-color: #7849b7;
  border: 1px solid #7849b7;
}

.reset_btn:focus {
  background-color: #7849b7;
  border: 1px solid #7849b7;
}
</style>
